﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace WP7_HackerTyper {
    public partial class MainPage : PhoneApplicationPage {
        public MainPage() {
            InitializeComponent();
        }

        private void btnHack_Click(object sender, RoutedEventArgs e) {
            if (ValidateIpAddress(txtIpAddress.Text.Trim())) {
                string navigationString = "/TerminalPage.xaml?speed=" + sldSpeed.Value.ToString();
                navigationString += "&ipadr=" + txtIpAddress.Text.Trim();
                //string navigationString = "/HackingTermPage.xaml";
                NavigationService.Navigate(new Uri(navigationString, UriKind.Relative));
            }
            else {
                MessageBox.Show("Type a valid IP Address");
            }
        }

        private bool ValidateIpAddress(string ip) {
            string[] values = ip.Split(".".ToArray());
            if (values.Length != 4)
                return false;

            if (ip.Equals("127.0.0.1") || ip.Contains("255"))
                return false;

            int tmp;
            for(int i = 0; i<4; i++) {
                if (!int.TryParse(values[i], out tmp)) {
                    return false;
                }
                else {
                    if (!(1 <= tmp && tmp <= 256))
                        return false;
                }
            }
            return true;
        }
    }
}